<template>
  <div>
    <a-row type="flex" :gutter="10">
      <a-col :span="12" style="height: calc(100vh - 125px)">
        <a-card :bordered="false" style="min-height: calc(100vh - 125px)">
          <!-- 条件搜索 -->
          <div class="table-page-search-wrapper">
            <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
              <a-row :gutter="32">
                <a-col :span="12">
                  <a-form-item label="帖子状态">
                    <a-select placeholder="请选择帖子状态" v-model="queryParam.status" style="width: 100%" allow-clear>
                      <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.dictValue" @click="handleQuery">{{ d.dictLabel }}</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced" v-hasPermi="['platform:region:add']">
                  <a-form-item label="用户">
                    <select-user v-model="queryParam.userId" select-model="single" placeholder="请选择用户" />
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced" v-hasPermi="['platform:region:add']">
                  <a-form-item label="地区">
                    <select-region v-model="queryParam.regionId" placeholder="请选择地区" />
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced">
                  <a-form-item label="版块">
                    <select-section v-model="queryParam.sectionId" placeholder="请选择版块" />
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced">
                  <a-form-item label="帖子标题">
                    <a-input v-model="queryParam.title" placeholder="请输入帖子标题" allow-clear @keyup.enter.native="handleQuery" />
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced">
                  <a-form-item label="帖子内容">
                    <a-input v-model="queryParam.content" placeholder="请输入帖子内容" allow-clear @keyup.enter.native="handleQuery" />
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced">
                  <a-form-item label="匿名状态">
                    <a-radio-group v-model="queryParam.anonymity" style="margin-top: 5px">
                      <a-radio v-for="(dict, index) in anonymityOptions" :key="index" :value="dict.dictValue" @click="radioAgain(dict.dictValue)">
                        {{ dict.dictLabel }}
                      </a-radio>
                    </a-radio-group>
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced">
                  <a-form-item label="帖子类型">
                    <a-select placeholder="请选择帖子类型" v-model="queryParam.type" style="width: 100%" allow-clear>
                      <a-select-option v-for="(d, index) in typeOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>

                <a-col>
                  <span class="table-page-search-submitButtons" style="float: right">
                    <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                    <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                    <a @click="toggleAdvanced" style="margin-left: 8px">
                      {{ advanced ? '收起' : '展开' }}
                      <a-icon :type="advanced ? 'up' : 'down'" />
                    </a>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
          <a-table
            :pagination="{
              current: queryParam.pageNum,
              pageSize: queryParam.pageSize,
              total: total,
              showSizeChanger: true,
              showLessItems: true,
              showQuickJumper: true,
              showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
              onChange: changeSize,
              onShowSizeChange: onShowSizeChange
            }"
            tableKey="base-region-index-table"
            @change="handleTableChange"
            rowKey="id"
            size="middle"
            @refresh="getList"
            :columns="columns"
            :data-source="postList"
            :loading="loading"
            :format-conditions="true"
            :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
            :scroll="{ x: 1500 }"
          >
            <span slot="status" slot-scope="text, record">
              <a-badge :status="statusBage(record.status)" :text="statusFormat(record)" />
            </span>
            <span slot="anonymity" slot-scope="text, record">
              {{ anonymityFormat(record) }}
            </span>
            <span slot="type" slot-scope="text, record">
              <a-tag :color="typeColor(record.type)">{{ typeFormat(record) }}</a-tag>
            </span>
            <span slot="image" slot-scope="text, record">
              <a-avatar shape="square" :src="imageUrl + record.image" />
            </span>
          </a-table>
        </a-card>
      </a-col>
      <a-col :span="12" style="height: calc(100vh - 125px); overflow-y: auto" id="box">
        <a-card :bordered="false" style="min-height: calc(100vh - 125px)">
          <post-detail :postId="id" @getList="getList" :typeOptions="typeOptions"></post-detail>
        </a-card>
      </a-col>
    </a-row>
    <a-back-top style="margin-right: -80px" :target="fn" />
  </div>
</template>

<script>
import { listPost, getInitData } from '@/api/platform/post'
import SelectUser from '@/components/pt/selectUser/SelectUser'
import SelectSection from '@/views/platform/components/SelectSection'
import SelectRegion from '@/views/platform/components/SelectRegion'
import PostDetail from './modules/PostDetail'
import PostApprove from './modules/PostApprove'
export default {
  name: 'PostApprove',
  components: {
    SelectUser,
    SelectSection,
    SelectRegion,
    PostDetail
  },
  data() {
    return {
      //图片链接
      imageUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      id: undefined,
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 10 },
      // 内容区域的百分比
      wrapperCol: { span: 14 },
      // 帖子管理表格数据
      postList: [],
      // 帖子状态字典
      statusOptions: [],
      anonymityOptions: [],
      typeOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        regionId: undefined,
        sectionId: undefined,
        title: undefined,
        content: undefined,
        type: undefined,
        status: '3',
        anonymity: undefined
      },
      columns: [
        {
          title: '用户',
          dataIndex: 'userName',
          ellipsis: true,
          align: 'left',
          width: '100px'
        },
        {
          title: '地区',
          dataIndex: 'regionName',
          ellipsis: true,
          align: 'left',
          width: '150px'
        },
        {
          title: '版块',
          dataIndex: 'sectionName',
          ellipsis: true,
          align: 'left',
          width: '150px'
        },
        {
          title: '帖子标题',
          dataIndex: 'title',
          ellipsis: true,
          align: 'left',
          width: '180px'
        },
        {
          title: '帖子内容',
          dataIndex: 'content',
          ellipsis: true,
          align: 'left',
          width: '250px'
        },
        {
          title: '帖子封面',
          dataIndex: 'image',
          scopedSlots: { customRender: 'image' },
          ellipsis: true,
          align: 'center',
          width: '100px'
        },
        {
          title: '联系人',
          dataIndex: 'contact',
          ellipsis: true,
          align: 'left',
          width: '80px'
        },
        {
          title: '联系电话',
          dataIndex: 'phone',
          ellipsis: true,
          align: 'left',
          width: '100px'
        },
        {
          title: '类型',
          dataIndex: 'type',
          ellipsis: true,
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'type' }
        },
        {
          title: '匿名状态',
          dataIndex: 'anonymity',
          ellipsis: true,
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'anonymity' }
        },
        {
          title: '帖子发布时间',
          dataIndex: 'publishTime',
          ellipsis: true,
          align: 'left',
          width: '150px'
        },
        {
          title: '帖子修改时间',
          dataIndex: 'editTime',
          ellipsis: true,
          align: 'left',
          width: '150px'
        },
        {
          title: '点赞数',
          dataIndex: 'likeNumber',
          ellipsis: true,
          align: 'center',
          width: '80px'
        },
        {
          title: '收藏数',
          dataIndex: 'collectNumber',
          ellipsis: true,
          align: 'center',
          width: '80px'
        },
        {
          title: '回复数',
          dataIndex: 'replyNumber',
          ellipsis: true,
          align: 'center',
          width: '80px'
        },
        {
          title: '浏览数',
          dataIndex: 'viewNumber',
          ellipsis: true,
          align: 'center',
          width: '80px'
        },
        {
          title: '举报数',
          dataIndex: 'reportNumber',
          ellipsis: true,
          align: 'center',
          width: '80px'
        },
        {
          title: '帖子状态',
          dataIndex: 'status',
          ellipsis: true,
          align: 'center',
          width: '120px',
          scopedSlots: { customRender: 'status' },
          fixed: 'right'
        }
      ]
    }
  },
  created() {
    this.getList()
    getInitData('approve_status,is_anonymity,post_type').then(response => {
      this.statusOptions = response.data.approve_status
      this.anonymityOptions = response.data.is_anonymity
      this.typeOptions = response.data.post_type
    })
  },
  methods: {
    ...PostApprove.methods,
    radioAgain(value) {
      if (this.queryParam.anonymity === value) {
        this.queryParam.anonymity = undefined
      }
    },
    /** 查询帖子管理列表 */
    getList() {
      this.loading = true
      this.id = undefined
      this.selectedRowKeys = []
      const param = this.queryParam
      if (this.queryParam.userId) {
        param.userId = this.queryParam.userId.ids
      }
      if (this.queryParam.sectionId) {
        param.sectionId = this.queryParam.sectionId.sectionId
      }
      listPost(param).then(response => {
        this.postList = response.data.rows
        this.total = response.data.total
        if (response.data.rows.length > 0) {
          this.selectedRowKeys.push(this.postList[0].id)
          this.id = this.postList[0].id
        }
        this.loading = false
      })
    },
    // 帖子状态字典翻译
    statusFormat(row) {
      if (row.status) {
        return this.selectDictLabel(this.statusOptions, row.status)
      } else {
        return ''
      }
    },
    // 帖子匿名状态字典翻译
    anonymityFormat(row) {
      if (row.anonymity) {
        return this.selectDictLabel(this.anonymityOptions, row.anonymity)
      } else {
        return ''
      }
    },
    typeFormat(row) {
      if (row.type) {
        return this.selectDictLabel(this.typeOptions, row.type)
      } else {
        return ''
      }
    },
    // 地区状态徽标数类型
    typeColor(type) {
      if (type === '0') {
        return 'blue'
      } else if (type === '1') {
        return 'green'
      } else if (type === '2') {
        return 'red'
      }
    },
    // 帖子状态徽标数类型
    statusBage(status) {
      if (status === '0') {
        return 'default'
      } else if (status === '1') {
        return 'processing'
      } else if (status === '2') {
        return 'warning'
      } else if (status === '3') {
        return 'error'
      } else if (status === '4') {
        return 'processing'
      } else if (status === '5') {
        return 'error'
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        regionId: undefined,
        sectionId: undefined,
        title: undefined,
        content: undefined,
        type: undefined,
        status: '3',
        anonymity: undefined
      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.id = this.selectedRows[0].id
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleTableChange(pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    }
  }
}
</script>
